package socksproxy.netty.proxy_wangZha.client;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;

public class SocketClient {
	public static void sendMsgBySocket(byte[] msg) {
		try {
			Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(
					"127.0.0.1", 55667));
			Socket socket = new Socket();
			socket.connect(proxy.address());
			socket.setKeepAlive(true);
			OutputStream out = socket.getOutputStream();
			ByteBuffer header = ByteBuffer.allocate(4);
			header.putInt(msg.length);
			out.write(header.array());
			out.write(msg);
			out.flush();
			InputStream in = socket.getInputStream();
			byte[] buff = new byte[4096];
			int readed = in.read(buff);
			if (readed > 0) {
				String str = new String(buff, 4, readed);
				System.out.println(str);
			}
			out.close();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		new SocketClient().sendMsgBySocket("明天天气怎么样".getBytes());
	}
}
